package it.uniroma2.art.owlart.langsearch.search.qcompilers;

import org.apache.lucene.queryParser.QueryParser;

/**
 * This query compiler is a simple processor which escape all the characters contained in the query. It is
 * thought for simple users which are not interested in using the advanced search modifiers of Lucene but want
 * to get all the characters in the queries processed as true characters.
 * 
 * This compiler can be used in two ways. By directly calling its specific methods, which take a special
 * modifier object as an input for adding more features to the search (while leaving a full escape of the
 * passed query), or through the standard {@link QueryCompiler#compile(String)} interface. In that case, an
 * internal modifier will be applied. The internal modifier can be set/get through getter/setter methods.
 * 
 * @author Armando Stellato &lt;stellato@info.uniroma2.it&gt;
 * 
 */
public class SimpleUserQueryCompiler implements QueryCompiler {

	public static enum QueryModifier {
		EXACT, STARTSWITH, ENDSWITH, CONTAINS
	}

	QueryModifier innerMod;

	public SimpleUserQueryCompiler() {
		innerMod = QueryModifier.EXACT;
	}

	public SimpleUserQueryCompiler(QueryModifier mod) {
		innerMod = mod;
	}

	public void setModifier(QueryModifier mod) {
		innerMod = mod;
	}

	public QueryModifier getModifier() {
		return innerMod;
	}

	public String compile(String query) {
		return compile(query, innerMod);
	}

	public String compile(String queryString, QueryModifier mod) {
		// TODO if it is the case to add the "" before or after the query modifiers
		// String processedQuery = "\"" + QueryParser.escape(queryString) + "\"";
		String processedQuery = QueryParser.escape(queryString);
		processedQuery = QueryParser.escape(queryString);
		processedQuery = processedQuery.replace(' ', '?');

		switch (mod) {
		case STARTSWITH:
			processedQuery = processedQuery + "*";
			// processedQuery = processedQuery + "*";
			break;
				
		case ENDSWITH:
			processedQuery = "*" + processedQuery;			
			break;
				
		case CONTAINS:
			processedQuery = "*" + processedQuery + "*";
			break;
			
		case EXACT:
			break;
		}
		
		return processedQuery;
	}

}
